package com.huasan.www.demon.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.Region;

//http://blog.csdn.net/yaohucaizi/article/details/8852872
//http://blog.csdn.net/ynztpwl/article/details/8468334
//http://blog.csdn.net/hoking_in/article/details/7919368 style

public class ExcelUtil {
	
	HSSFCellStyle cellStyle;
	
	public int writeXls(String title, String[] head, String[][] data, OutputStream out ) throws IOException{
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("Details");
		
		CellRangeAddress region = new CellRangeAddress(0, 0, (short) 0, (short) (head.length-1));
		sheet.addMergedRegion(region);
//		sheet.addMergedRegion( new Region(     
//                0, //first row (0-based)       
//               (short)0, //first column  (0-based)       
//                0, //last row (0-based)    
//               (short)(head.length-1)  //last column  (0-based)       
//        ));
		
		HSSFRow trow = sheet.createRow(0);
		setCell(workbook, trow, 0, title);
		
		HSSFRow hrow = sheet.createRow(1);
		for (int i=0; i<head.length; i++){
			sheet.setColumnWidth(i, 8000);
			setCell(workbook, hrow, i, head[i]);
		}
		
		for (int i=0; i<data.length; i++){
			HSSFRow row = sheet.createRow(i+2);
			for (int j=0; j<data[i].length; j++){
				setCell(workbook, row, j, data[i][j]);
			}
		}
		workbook.write(out);
		out.flush();
		return 1;
	}
	
	public int readXls(){
		return 1;
	}
	
	public void setStyle(HSSFCellStyle cellStyle){
		this.cellStyle = cellStyle;
	}
	
	public HSSFCellStyle createStyle(HSSFWorkbook workbook){
		cellStyle = workbook.createCellStyle();
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框  
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框  
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框  
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		return cellStyle;
	}
	
	public void setCell(HSSFWorkbook workbook, HSSFRow row, int num, String data){
		
		if (cellStyle==null)
			createStyle(workbook);
		
		HSSFCell cell = row.createCell(num);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(data);
		cell.setCellStyle(cellStyle);
	}
	
	/*
	 * HSSFFont font = wb.createFont();  
            font.setFontHeightInPoints((short) 10);  
            font.setFontName("新宋体");  
            font.setColor(HSSFColor.BLUE.index);  
            font.setBoldweight((short) 0.8); 
            
            HSSFCellStyle style = wb.createCellStyle();  
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
            style.setFont(font); //调用字体样式对象  
            style.setWrapText(true);  
                           //增加表格边框的样式 例子  
                           style.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);  
                       style.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);  
                       style.setTopBorderColor(HSSFColor.GOLD.index);  
                       style.setLeftBorderColor(HSSFColor.PLUM.index); 
	 * */
	
}
